package com.hoshiibuy.spu.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.hoshiibuy.common.rpc.Response;
import com.hoshiibuy.spu.entity.ExtSpuProhibit;
import com.hoshiibuy.spu.param.ExtSpuProhibitPageParam;
import com.hoshiibuy.spu.param.ExtSpuProhibitUpdateParam;
import com.hoshiibuy.spu.service.IExtSpuProhibitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * SPU禁售地区Controller类
 *
 * @author : yangcheng
 * @date : 2019/5/5 11:19
 * @mail : maiboyc@163.com
 **/
@RestController
@RequestMapping("/spuProhibit")
@Api(value = "SPU禁售地区管理", tags = {"SPU禁售地区操作接口"}, produces = MediaType.APPLICATION_JSON_VALUE)
public class ExtSpuProhibitController extends BaseController {

    // SPU禁售地区Service
    @Autowired
    private IExtSpuProhibitService ExtSpuProhibitService;

    /**
     * 根据ID查询SPU禁售地区资料信息
     * @param id SPU禁售地区资料ID
     * @return Response
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询", notes = "根据ID查询SPU禁售地区资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "SPU禁售地区资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response get(@PathVariable(name = "id") Long id) {
        return Response.data(ExtSpuProhibitService.get(id));
    }

    /**
     * 根据地区ID查询SPU禁售地区资料信息
     * @param zoneId  SPU禁售地区ID
     * @return Response
     */
    @GetMapping("/find/{zoneId}")
    @ApiOperation(value = "根据禁售地区ID查询", notes = "根据禁售地区ID查询SPU禁售地区资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "zoneId", value = "SPU禁售地区ID", required = true, dataType = "String", example = "衬衫", paramType = "query")
    public Response findSpuByZoneId(@PathVariable(name = "zoneId") String zoneId) {
        Wrapper wrapper = Wrappers.<ExtSpuProhibit>query().lambda().eq(ExtSpuProhibit::getZoneid, zoneId);
        return Response.data(ExtSpuProhibitService.getOne(wrapper));
    }

    /**
     * 分页查询SPU禁售地区资料信息（get请求，以具体值接收参数）
     * @param pageIndex  当前页
     * @param pageSize   每页大小
     * @param spuId      spuId
     * @param zoneId     地区ID
     * @return Response
     */
    @GetMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询SPU禁售地区资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页码", required = true, dataType = "Long", example = "1", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", required = true, dataType = "Long", example = "10", paramType = "query"),
            @ApiImplicitParam(name = "spuId", value = "spu标题", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "zoneId", value = "售后说明", dataType = "String", paramType = "query")
    })
    public Response list(Long pageIndex, Long pageSize, Long spuId, Long zoneId) {
        ExtSpuProhibit entity = new ExtSpuProhibit();
        entity.setPageIndex(pageIndex);
        entity.setPageSize(pageSize);
        entity.setSpuid(spuId);
        entity.setZoneid(zoneId);
        return Response.data(ExtSpuProhibitService.List(entity));
    }

    /**
     * 分页查询SPU禁售地区资料信息（post请求，以对象接收参数）
     * @param param 查询参数实体
     * @return Response
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页查询", notes = "分页查询SPU禁售地区资料信息", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response list(@RequestBody ExtSpuProhibitPageParam param) {
        ExtSpuProhibit entity = new ExtSpuProhibit();
        BeanUtils.copyProperties(param, entity);
        return Response.data(ExtSpuProhibitService.List(entity));
    }

    /**
     * 新增SPU禁售地区资料
     * @param entity 保存参数实体
     * @return Response
     */
    @PostMapping("/save")
    @ApiOperation(value = "新增SPU禁售地区资料", notes = "新增SPU禁售地区资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response saveSpu(@RequestBody @Valid ExtSpuProhibit entity, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                rs = Response.data(ExtSpuProhibitService.save(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 修改SPU禁售地区资料
     * @param param 修改参数实体
     * @return Response
     */
    @PutMapping("/update")
    @ApiOperation(value = "修改SPU禁售地区资料", notes = "修改SPU禁售地区资料", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Response updateSpuById(@RequestBody @Valid ExtSpuProhibitUpdateParam param, BindingResult result) {
        Response rs = new Response();
        try {
            if (CollectionUtils.isNotEmpty(handleValidMsg(result))){// 验证不通过
                rs = Response.failed(objectMapper.writeValueAsString(handleValidMsg(result)));
            } else {// 验证通过
                ExtSpuProhibit entity = new ExtSpuProhibit();
                BeanUtils.copyProperties(param, entity);
                rs = Response.data(ExtSpuProhibitService.update(entity));
            }
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return rs;
    }

    /**
     * 根据ID删除SPU禁售地区资料信息
     * @param id SPU禁售地区资料ID
     * @return Response
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据ID删除", notes = "根据ID删除SPU禁售地区资料信息", consumes = MediaType.ALL_VALUE)
    @ApiImplicitParam(name = "id", value = "SPU禁售地区资料主键ID", required = true, dataType = "String", example = "3187243209717710848", paramType = "query")
    public Response deleteById(Long id) {
        return Response.data(ExtSpuProhibitService.delete(id));
    }

}  